package com.example.juc.interview;

import java.util.Arrays;

/**
 * 选择排序
 *
 * @author jl
 * @since 2021/11/9 13:36
 */
public class SelectionSort {

    public static int[] sort(int[] array) {
        // 倒叙遍历
        for (int i = array.length - 1; i >= 0; i--) {
            int max = Integer.MIN_VALUE;
            int maxIndex = 0;
            for (int j = 0; j < (i + 1); j++) {
                if (array[j] > max) {
                    max = array[j];
                    maxIndex = j;
                }
            }

            int temp = array[i];
            array[i] = array[maxIndex];
            array[maxIndex] = temp;
        }
        return array;
    }

    public static void main(String[] args) {
        int[] array = new int[]{16, 15, 14};
        Arrays.stream(sort(array)).forEach(System.out::println);
    }
}
